package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.PatientDetail;
import com.qilu.pe.dao.bean.Test;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.GlideUtil;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 患者检测项目详情
public class PatientTestItemDetailActivity extends Base2Activity {
    protected RecyclerView rcv_test_item;
    private TextView tv_name, tv_sex, tv_age, tv_doctor;
    private ImageView iv_sex, iv_header;

    private TestItemListAdapter adapter;
    private List<Test> testList = new ArrayList<>();

    private String id;
    private PatientDetail detail;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_patient_test_item_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_test_item = findViewById(R.id.rcv_test_item);
        tv_name = findViewById(R.id.tv_name);
        tv_sex = findViewById(R.id.tv_sex);
        tv_age = findViewById(R.id.tv_age);
        tv_doctor = findViewById(R.id.tv_doctor);
        iv_sex = findViewById(R.id.iv_sex);
        iv_header = findViewById(R.id.iv_header);

        setNavigationIcon(R.mipmap.ic_left_arrow);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            id = bundle.getString("id");
            detail = (PatientDetail) bundle.getSerializable("patientDetail");
        }

        if (null != detail) {
            tv_name.setText(detail.getName());
            tv_sex.setText(TextUtils.equals("1", detail.getSex()) ? "女" : "男");
            tv_age.setText("年龄:" + detail.getAge());
            tv_doctor.setText("负责人:" + detail.getU_name());
//            tv_phone.setText(detail.getPhone());
//            tv_history_info.setText(detail.getMedical());
//            tv_special_info.setText(detail.getRemark());
            iv_sex.setImageResource(TextUtils.equals("1", detail.getSex()) ?
                    R.mipmap.ic_patient_sex_female : R.mipmap.ic_patient_sex_male);// ic_patient_sex_female ic_patient_sex_male
            GlideUtil.loadImg(detail.getPicture(), iv_header);
        }

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new TestItemListAdapter(this);
        rcv_test_item.setLayoutManager(manager);
        rcv_test_item.setAdapter(adapter);

        for (int i = 0; i < 2; i++) {
            testList.add(new Test());
        }
        adapter.setDataList(testList);

        setHeadTitle("患者项目详情");

        hideProgress();
        reqProductOrderList();
        showProgress();
    }

    private Disposable reqProductOrderList() {
        return APIRetrofit.getDefault().reqProductOrderList(Global.HEADER, id)
                .compose(RxUtil.<BaseResult2<List<Test>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Test>>>() {
                    @Override
                    public void accept(BaseResult2<List<Test>> listBaseResult2) throws Exception {
                        hideProgress();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class TestItemListAdapter extends ListBaseAdapter<Test> {

        public TestItemListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_test_item_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {

        }
    }
}